package org.platform.core.filecommand;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.platform.tool.helper.ResourceHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.io.File;


/**
 * Created by mabb on 15-1-23.
 */
//@Service
@Slf4j
@Deprecated
public class FileCommandInit implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private ResourceHelper resource;
    @Autowired
    private BaseFileCommandListener listener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent ctxEvent) {
        FileAlterationMonitor monitor = new FileAlterationMonitor(1000 * 2);

        /**
         * 此处输入要监听的目录，根据不同的业务
         * 需要通过配置文件以及类读取
         */
        try {
            //TODO 这里怎么定义本部?接收站?

            String workDir = resource.getLocalRootDir() + resource.getLocalArchivingDir();
            File temp = new File(workDir);
            if (!temp.exists()) {
                if (!temp.mkdirs()) {
                    throw new RuntimeException("创建监听文件夹 :" + workDir + " 失败");
                }
            }
            FileAlterationObserver workObserver = new FileAlterationObserver(workDir);
            workObserver.addListener(listener);
            monitor.addObserver(workObserver);
            monitor.start();
        } catch (Exception e) {
            log.error("异常:", e);
        }

    }
}
